package cn.com.headfree.checkrepeat.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.checkrepeat.service.CheckRepeatService;
import cn.com.headfree.checkrepeat.vo.*;
import cn.com.headfree.entity.TPartRank;
import cn.com.headfree.entity.TTecStandardInfo;
import cn.com.headfree.entity.isc.User;
import com.gta.edu.sdk.common.dto.Page;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;


@Controller
@RequestMapping(value = "/checkRepeat", produces = "application/json;charset=UTF-8")
public class CheckRepeatController extends ExBaseController {

	private static final Logger LOG = LogManager.getLogger(CheckRepeatController.class);
	@Autowired
	public CheckRepeatService checkRepeatService;


	/**
	 * 学术成果
	 * @param name
	 * @return
	 */
	@RequestMapping("/getAcadeAchiveInfoByName")
	@ResponseBody
	public ResultData getAcadeAchiveInfoByName(HttpServletRequest request,Page<TAcadeAchiveInfoVo> page, String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getAcadeAchiveInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询学术成果成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询学术成果失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 标准制定
	 * @param name
	 * @return
	 */
	@RequestMapping("/getStandInfoByName")
	@ResponseBody
	public ResultData getStandInfoByName(HttpServletRequest request,Page<TStandInfoVo> page,String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getStandInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询标准制定成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询标准制定失败！");
			e.printStackTrace();
		}
		return rs;
	}


	/**
	 * 专利
	 * @param name
	 * @return
	 */
	@RequestMapping("/getPatentInfoByName")
	@ResponseBody
	public ResultData getPatentInfoByName(HttpServletRequest request,Page<TPatentInfoVo> page,String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getPatentInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询专利成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询专利失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 技艺革新
	 * @param name
	 * @return
	 */
	@RequestMapping("/getTecInnovInfoByName")
	@ResponseBody
	public ResultData getTecInnovInfoByName(HttpServletRequest request,Page<TTecInnovInfoVo> page,String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getTecInnovInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询技艺革新成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询技艺革新失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 科技管理创新
	 * @param name
	 * @return
	 */
	@RequestMapping("/getTecManInnovInfoByName")
	@ResponseBody
	public ResultData getTecManInnovInfoByName(HttpServletRequest request,Page<TTecManInnovVo> page,String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getTecManInnovInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询科技管理创新成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询科技管理创新失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 项目课题成果
	 * @param name
	 * @return
	 */
	@RequestMapping("/getProjAchieveInfoByName")
	@ResponseBody
	public ResultData getProjAchieveInfoByName(HttpServletRequest request,Page<TProjAchieveInfoVo> page,String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getProjAchieveInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询项目课题成果成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询项目课题成果失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 技术标准
	 * @param name
	 * @return
	 */
	@RequestMapping("/getTecStandardInfoByName")
	@ResponseBody
	public ResultData getTecStandardInfoByName(HttpServletRequest request, Page<TTecStandardInfo> page, String name) {
		ResultData rs = new ResultData();
		User user = this.getLoginUserInfo(request);
		if(user == null){
			rs.setSuccess(false);
			rs.setErrorMsg("获取当前登录人失败");
			return rs;
		}
		try {
			checkRepeatService.getTecStandardInfoByName(page,name,user.getBaseOrgId());
			rs.setData(page);
			rs.setSuccess(true);
			LOG.info("查询技术标准成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查询技术标准失败！");
			e.printStackTrace();
		}
		return rs;
	}
}
